/********************************************************************************
 * CreateWantedAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Nov 11, 2008 4:20:03 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.printeverywhere.delegator.AuctionServiceDelegator;
import com.printeverywhere.delegator.CategoryManagerServiceDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.CurrencyServiceDelegator;
import com.printeverywhere.delegator.ItemsServiceDelegator;
import com.printeverywhere.delegator.LocationCategoriesServiceDelegator;
import com.printeverywhere.delegator.TranslationSystemServiceDelegator;
import com.printeverywhere.dto.AdditionalCategories;
import com.printeverywhere.dto.AdditionalOptionsFees;
import com.printeverywhere.dto.AuctionListingFees;
import com.printeverywhere.dto.BidStatus;
import com.printeverywhere.dto.Currency;
import com.printeverywhere.dto.GeneralCategoriesWOS;
import com.printeverywhere.dto.GeneralOptions;
import com.printeverywhere.dto.ItemAuctionImage;
import com.printeverywhere.dto.ItemAuctionImageId;
import com.printeverywhere.dto.ItemCategories;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.Itemtype;
import com.printeverywhere.dto.Languages;
import com.printeverywhere.dto.ListingOptions;
import com.printeverywhere.dto.LocationCategoriesWOS;
import com.printeverywhere.dto.MiscellaneousFees;
import com.printeverywhere.dto.Transactions;
import com.printeverywhere.dto.UploadedFile;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.CommonUtil;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SessionUtil;

public class CreateWantedAction extends FrontBaseSupport implements
		ServletRequestAware, ServletContextAware {

	/**  */
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private List<Currency> currency = CurrencyServiceDelegator.getList();

	/** The region list. */
	private List<LocationCategoriesWOS> regionList = getRegionListByLevel();

	/** The state list. */
	private List<LocationCategoriesWOS> countryList = new ArrayList<LocationCategoriesWOS>();

	/** The state list. */
	private List<LocationCategoriesWOS> stateList = new ArrayList<LocationCategoriesWOS>();

	private File upload;

	private double fees;

	private double regularInsertionFees;

	private List<UploadedFile> uploadedFiles = new ArrayList<UploadedFile>();

	private List<Items> featuredItems = new ArrayList<Items>();

	private Items items;

	private String uploadContentType;

	private String uploadFileName;

	private String action = AdminConstants.NONE;

	private User user;

	private double fileFees;

	private String[] categories;

	private String[] backCategories;

	private String backAction;

	private Integer thumbnailId;

	private Integer gallaryId;

	private String defaultLang;

	private int defaultRegion;

	private int defaultCountry;

	private String defaultCurrency;

	private ServletContext servletContext;
	
	private List<Criterion> criterionList = new ArrayList<Criterion>();
	
	private String[] editCategories;
	
	private String[] editbackCategories;
	
	private List<Languages> languageList;

	public String[] getEditbackCategories() {
		return editbackCategories;
	}

	public void setEditbackCategories(String[] editbackCategories) {
		this.editbackCategories = editbackCategories;
	}

	public String[] getEditCategories() {
		return editCategories;
	}

	public void setEditCategories(String[] editCategories) {
		this.editCategories = editCategories;
	}

	public String backAction() {
		this.items = (Items) ActionContext.getContext().getSession().get("items");
		this.categories = (String[]) ActionContext.getContext().getSession().get("categories");
		this.backCategories = (String[]) ActionContext.getContext().getSession().get("backCategories");
		this.uploadedFiles = (List<UploadedFile>) ActionContext.getContext().getSession().get("uploadedFiles");

		countryList = LocationCategoriesServiceDelegator.getCountryListByRegionIdWOS(this.items.getLocationCategoriesByRegion().getCategoryId());
		stateList = LocationCategoriesServiceDelegator.getStateListByCountryIdWOS(this.items.getLocationCategoriesByCountry().getCategoryId());

		setDefaultValues();
		return SUCCESS;
	}

	public ServletContext getServletContext() {
		return servletContext;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public String[] getCategories() {
		return categories;
	}

	public void setCategories(String[] categories) {
		this.categories = categories;
	}

	public Integer getThumbnailId() {
		return thumbnailId;
	}

	public void setThumbnailId(Integer thumbnailId) {
		this.thumbnailId = thumbnailId;
	}

	public Integer getGallaryId() {
		return gallaryId;
	}

	public void setGallaryId(Integer gallaryId) {
		this.gallaryId = gallaryId;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getAction() {
		return action;
	}

	public Items getItems() {
		return items;
	}

	public void setItems(Items items) {
		this.items = items;
	}

	public CreateWantedAction() {
		super(false, false);
	}

	public String execute() {
		try {
			if (request.getParameter("cid") != null) {
				String itemId = request.getParameter("cid");
				this.items = ItemsServiceDelegator.getItemsById(Integer.parseInt(itemId));
				user = items.getUserId();
				countryList = LocationCategoriesServiceDelegator.getCountryListByRegionIdWOS(user.getRegion().getCategoryId());
				stateList = LocationCategoriesServiceDelegator.getStateListByCountryIdWOS(user.getCountry().getCategoryId());
				
				if(criterionList.size() > 0){
	                 criterionList.clear();
	            }
				
				 criterionList.add(Restrictions.eq("itemId", this.items));
				 List<ItemCategories> itemCategoriesList = (List<ItemCategories>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, ItemCategories.class);
				
				 if(itemCategoriesList != null && itemCategoriesList.size() > 0){
	                 int index = 0;
	                 editCategories = new String[itemCategoriesList.size()];
	                 for (Iterator iterator = itemCategoriesList.iterator(); iterator.hasNext();) {
	                      ItemCategories itemCategories = (ItemCategories) iterator.next();
	                      int categoryId = itemCategories.getCategoryId().getCategoryId();
	                      editCategories[index] = String.valueOf(categoryId);
	                      index++;
	                 }
	            }
				 
				 if(editCategories != null && editCategories.length > 0){
		            	editbackCategories = new String[itemCategoriesList.size()];
			            for (int index = 0; index < editCategories.length; index++) {
			                 DynamicTreeSelectAction dynamicTreeSelectAction = new DynamicTreeSelectAction();
			                 String[] categoryList = dynamicTreeSelectAction.getGeneralCategoriesFromCategoryId(Integer.parseInt(editCategories[index]));
			                 editbackCategories[index] = "";

			                 for (String str : categoryList) {
			                	 editbackCategories[index] += (str + "->");
			                 }
			                 editbackCategories[index] = editbackCategories[index].substring(0, editbackCategories[index].length() - 2);
			            }
			        }
				 
				if ((request.getParameter("action") != null) && (request.getParameter("action").trim().equalsIgnoreCase("copyitem"))) {
					this.action = "copyCreateWanted";
				}
			}
			ActionContext.getContext().getSession().put("uploadedFiles", null);
			setDefaultValues();
		} catch (Exception e) {
			return INPUT;
		}
		return SUCCESS;
	}

	public String featuredWanted() {
		setLeftAllowed(true);
		setRightAllowed(true);
		setCategoryDisplay(true);

		List<Criterion> criterionList = new ArrayList<Criterion>();
		criterionList.add(Restrictions.eq("categoryFeatured", Boolean.TRUE));
		criterionList.add(Restrictions.ge("endTime", DateUtil.getTodaysDate()));
		criterionList.add(Restrictions.eq("itemtype.id", 4));

		if(request.getSession().getAttribute("categoryInSession") != null) {
			GeneralCategoriesWOS category = (GeneralCategoriesWOS) CommonFunctionDelegator.getObjectById("categoryId", Integer.parseInt(request.getSession().getAttribute("categoryInSession").toString()), GeneralCategoriesWOS.class);
			List<Criterion> tmpCriterionList = new ArrayList<Criterion>();
			tmpCriterionList.add(Restrictions.ge("categoryId", category.getCategoryId()));
			tmpCriterionList.add(Restrictions.eq("mainParentId", category.getMainParentId()));
			criterionList.add(Restrictions.in("generalCategories", CommonFunctionDelegator.getObjectsByCriterion(tmpCriterionList, null, null, GeneralCategoriesWOS.class)));
		}

		this.featuredItems = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

		return SUCCESS;
	}

	public String featuredTender() {
		setLeftAllowed(true);
		setRightAllowed(true);
		setCategoryDisplay(true);

		List<Criterion> criterionList = new ArrayList<Criterion>();
		criterionList.add(Restrictions.eq("categoryFeatured", Boolean.TRUE));
		criterionList.add(Restrictions.ge("endTime", DateUtil.getTodaysDate()));
		criterionList.add(Restrictions.eq("itemtype.id", 7));

		if(request.getSession().getAttribute("categoryInSession") != null) {
			GeneralCategoriesWOS category = (GeneralCategoriesWOS) CommonFunctionDelegator.getObjectById("categoryId", Integer.parseInt(request.getSession().getAttribute("categoryInSession").toString()), GeneralCategoriesWOS.class);
			List<Criterion> tmpCriterionList = new ArrayList<Criterion>();
			tmpCriterionList.add(Restrictions.ge("categoryId", category.getCategoryId()));
			tmpCriterionList.add(Restrictions.eq("mainParentId", category.getMainParentId()));
			criterionList.add(Restrictions.in("generalCategories", CommonFunctionDelegator.getObjectsByCriterion(tmpCriterionList, null, null, GeneralCategoriesWOS.class)));
		}

		this.featuredItems = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

		return SUCCESS;
	}

	public String uploadFile() {

		ListingOptions listingOptions = (ListingOptions) ActionContext
				.getContext().getApplication().get(
						AdminConstants.LISTING_OPTIONS);
		if (request.getParameter("deleteAction") != null) {
			if (ActionContext.getContext().getSession().get("uploadedFiles") != null) {
				uploadedFiles = (List<UploadedFile>) ActionContext.getContext()
						.getSession().get("uploadedFiles");
				uploadedFiles.remove(Integer.parseInt(request
						.getParameter("action")));
				ActionContext.getContext().getSession().put("uploadedFiles",
						uploadedFiles);
			}
		} else {
			File tempFile = null;
			try {
				if (ActionContext.getContext().getSession()
						.get("uploadedFiles") != null) {
					uploadedFiles = (List<UploadedFile>) ActionContext
							.getContext().getSession().get("uploadedFiles");
				}

				int totalFileSize = 0;
				for (UploadedFile uploadedFile : uploadedFiles) {
					totalFileSize += (uploadedFile.getSize() / 1024);
				}

				List<AdditionalOptionsFees> additionalOptionsFeesList = (List<AdditionalOptionsFees>) ActionContext
						.getContext().getApplication().get(
								AdminConstants.OPTIONS_FEES);

				if (uploadedFiles.size() >= listingOptions
						.getNumberOfFilesAllowedToUpload()) {
					addActionError("You can not upload more than "
							+ listingOptions.getNumberOfFilesAllowedToUpload()
							+ " files.");
				} else if (upload != null && (upload.length() / 1024) > listingOptions
						.getMaximumUploadFileSize()) {
					addActionError("You can not upload file with size more than "
							+ listingOptions.getMaximumUploadFileSize()
							+ " Kb.");
				} else if (listingOptions.getAllowAnyFileTypeToUpload().equalsIgnoreCase("false") && !isCorrectFileExtention(listingOptions
						.getFilesExtensionsAllowed(), getUploadFileName())) {
					addActionError("You can not upload file other than "
							+ listingOptions.getFilesExtensionsAllowed());
				} else if (totalFileSize > listingOptions
						.getMaximumUploadTotalFilesSize()) {
					addActionError("Your total upload size exeeds "
							+ listingOptions.getFilesExtensionsAllowed()
							+ " Kb.");
				} else {
					String method = "getWanted";
					int type = Integer.parseInt(request.getParameter("type"));
					if (type == 7)
						method = "getTender";

					UploadedFile uploadedFile = new UploadedFile();
					tempFile = File.createTempFile("MyFile.txt", ".tmp");
					if (upload != null) {
						float fileSize = (float) ((upload.length()) / 1024.00);
						if (fileSize == 0
								|| fileSize > listingOptions
										.getMaximumUploadFileSize()) {
							addActionError("File size is greater than maximum upload size");
							return INPUT;
						}
						FileUtils.copyFile(upload, tempFile);
						uploadedFile.setName(tempFile.getName());
						uploadedFile.setSize(tempFile.length());
						uploadedFile.setContentType(getUploadContentType());
						uploadedFiles.add(uploadedFile);
						if (uploadedFiles.size() == 1) {
							this.fileFees = Double.valueOf(CommonUtil
									.callMethod(AdditionalOptionsFees.class,
											additionalOptionsFeesList.get(10),
											method).toString());
						} else if (uploadedFiles.size() == 2) {
							this.fileFees = Double.valueOf(CommonUtil
									.callMethod(AdditionalOptionsFees.class,
											additionalOptionsFeesList.get(11),
											method).toString());
						} else if (uploadedFiles.size() == 3) {
							this.fileFees = Double.valueOf(CommonUtil
									.callMethod(AdditionalOptionsFees.class,
											additionalOptionsFeesList.get(12),
											method).toString());
						} else if (uploadedFiles.size() == 4) {
							this.fileFees = Double.valueOf(CommonUtil
									.callMethod(AdditionalOptionsFees.class,
											additionalOptionsFeesList.get(13),
											method).toString());
						} else if (uploadedFiles.size() >= 5) {
							this.fileFees = Double.valueOf(CommonUtil
									.callMethod(AdditionalOptionsFees.class,
											additionalOptionsFeesList.get(14),
											method).toString());
						}
						ActionContext.getContext().getSession().put(
								"uploadedFiles", uploadedFiles);
					} else {
						addActionError("Select image for upload");
						return INPUT;
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return INPUT;
	}

	public String showAdververtisement() {
		setRightAllowed(true);
		setLeftAllowed(true);
		setCategoryDisplay(true);

		List<Criterion> criterionList = new ArrayList<Criterion>();
		criterionList.add(Restrictions.eq("categoryFeatured", Boolean.TRUE));
		criterionList.add(Restrictions.ge("endTime", DateUtil.getTodaysDate()));
		criterionList.add(Restrictions.eq("itemtype.id", 5));

		if(request.getSession().getAttribute("categoryInSession") != null) {
			GeneralCategoriesWOS category = (GeneralCategoriesWOS) CommonFunctionDelegator.getObjectById("categoryId", Integer.parseInt(request.getSession().getAttribute("categoryInSession").toString()), GeneralCategoriesWOS.class);
			List<Criterion> tmpCriterionList = new ArrayList<Criterion>();
			tmpCriterionList.add(Restrictions.ge("categoryId", category.getCategoryId()));
			tmpCriterionList.add(Restrictions.eq("mainParentId", category.getMainParentId()));
			criterionList.add(Restrictions.in("generalCategories", CommonFunctionDelegator.getObjectsByCriterion(tmpCriterionList, null, null, GeneralCategoriesWOS.class)));
		}

		this.featuredItems = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

		return SUCCESS;
	}

	public String previewWanted() {
		this.backAction = "backToWanted";

		if (request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("copyCreateWanted")) {
			this.action = "copyCreateWanted";
		} else if (request.getParameter("items.itemId") != null && !request.getParameter("items.itemId").equals("")) {
			this.action = "updateCreateWanted";
		}else {
			this.action = "submitCreateWanted";
		}

		if (request.getParameter("cid") != null && request.getParameter("action").trim().equalsIgnoreCase("submitsimilar")) {
			String itemId = request.getParameter("cid");
			this.items = ItemsServiceDelegator.getItemsById(Integer.parseInt(itemId));
			this.action = "submitCreateWanted";
		}

		request.getSession().setAttribute("itemId", this.items.getItemId());
		
		if(request.getParameterValues("categoryId") != null){
            categories = request.getParameterValues("categoryId");
        }else {
            if(criterionList.size() > 0){
                criterionList.clear();
           }

           criterionList.add(Restrictions.eq("itemId", this.items));
           List<ItemCategories> itemCategoriesList = (List<ItemCategories>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, ItemCategories.class);
           if(itemCategoriesList != null && itemCategoriesList.size() > 0){
                int index = 0;
                categories = new String[itemCategoriesList.size()];
                for (Iterator iterator = itemCategoriesList.iterator(); iterator.hasNext();) {
                     ItemCategories itemCategories = (ItemCategories) iterator.next();
                     int categoryId = itemCategories.getCategoryId().getCategoryId();
                     categories[index] = String.valueOf(categoryId);
                     index++;
                }
           }
       }
		
		 if(categories != null && categories.length > 0){
	            this.backCategories = new String[categories.length];

	            for (int index = 0; index < categories.length; index++) {
	                 DynamicTreeSelectAction dynamicTreeSelectAction = new DynamicTreeSelectAction();
	                 String[] categoryList = dynamicTreeSelectAction.getGeneralCategoriesFromCategoryId(Integer.parseInt(categories[index]));
	                 backCategories[index] = "";

	                 for (String str : categoryList) {
	                	 backCategories[index] += (str + "->");
	                 }

	                 backCategories[index] = backCategories[index].substring(0, backCategories[index].length() - 2);
	            }
	        }

		if (ActionContext.getContext().getSession().get("uploadedFiles") != null) {
			this.uploadedFiles = (List<UploadedFile>) ActionContext
					.getContext().getSession().get("uploadedFiles");
		}

		try {
			if (this.thumbnailId != null) {
				File file = new File(System.getProperty("java.io.tmpdir")
						+ "\\"
						+ this.uploadedFiles.get(this.thumbnailId).getName());
				this.items.setThumbImage(CommonUtil.getBytesFromFile(file));
			}

			if (this.gallaryId != null) {
				File file = new File(System.getProperty("java.io.tmpdir")
						+ "\\"
						+ this.uploadedFiles.get(this.thumbnailId).getName());
				this.items.setGalleryImage(CommonUtil.getBytesFromFile(file));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.items
				.setLocationCategoriesByRegion(LocationCategoriesServiceDelegator
						.getWOS(this.items.getLocationCategoriesByRegion()
								.getCategoryId()));
		this.items
				.setLocationCategoriesByCountry(LocationCategoriesServiceDelegator
						.getWOS(this.items.getLocationCategoriesByCountry()
								.getCategoryId()));
		this.items
				.setLocationCategoriesByState(LocationCategoriesServiceDelegator
						.getWOS(this.items.getLocationCategoriesByState()
								.getCategoryId()));
		this.items.setItemLanguage(TranslationSystemServiceDelegator.get(this.items.getItemLanguage().getLanguageCode()));

		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.setAttribute("id", 4);
		List<Itemtype> itemTypeList = (List<Itemtype>) CommonFunctionDelegator
				.getObjectBySearchCriteria(searchCriteria, Itemtype.class);
		if (itemTypeList != null)
			this.items.setItemtype((Itemtype) itemTypeList.get(0));

		((List<AdditionalCategories>) ActionContext.getContext()
				.getApplication().get(AdminConstants.CATEGORY_FEES)).get(0)
				.getAuction();
		ActionContext.getContext().getSession().put("items", this.items);
		ActionContext.getContext().getSession().put("categories", categories);
		ActionContext.getContext().getSession().put("backCategories", this.backCategories);

		feesCalculation(4);

		return SUCCESS;
	}

	public String previewTender() {
		this.backAction = "backToTender";


		if (request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("copyCreateTender")) {
			this.action = "copyCreateTender";
		} else if (request.getParameter("items.itemId") != null && !request.getParameter("items.itemId").equals("")) {
			this.action = "updateCreateTender";
		} else {
			this.action = "submitCreateTender";
		}

		if (request.getParameter("cid") != null && request.getParameter("action").trim().equalsIgnoreCase("submitsimilar")) {
			String itemId = request.getParameter("cid");
			this.items = ItemsServiceDelegator.getItemsById(Integer.parseInt(itemId));
			this.action = "submitCreateTender";
		}

		
		request.getSession().setAttribute("itemId", this.items.getItemId());
		
		if(request.getParameterValues("categoryId") != null){
            categories = request.getParameterValues("categoryId");
        }else {
            if(criterionList.size() > 0){
                criterionList.clear();
           }

           criterionList.add(Restrictions.eq("itemId", this.items));
           List<ItemCategories> itemCategoriesList = (List<ItemCategories>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, ItemCategories.class);
           if(itemCategoriesList != null && itemCategoriesList.size() > 0){
                int index = 0;
                categories = new String[itemCategoriesList.size()];
                for (Iterator iterator = itemCategoriesList.iterator(); iterator.hasNext();) {
                     ItemCategories itemCategories = (ItemCategories) iterator.next();
                     int categoryId = itemCategories.getCategoryId().getCategoryId();
                     categories[index] = String.valueOf(categoryId);
                     index++;
                }
           }
       }

		if(categories != null && categories.length > 0){
            this.backCategories = new String[categories.length];

            for (int index = 0; index < categories.length; index++) {
                 DynamicTreeSelectAction dynamicTreeSelectAction = new DynamicTreeSelectAction();
                 String[] categoryList = dynamicTreeSelectAction.getGeneralCategoriesFromCategoryId(Integer.parseInt(categories[index]));
                 backCategories[index] = "";

                 for (String str : categoryList) {
                	 backCategories[index] += (str + "->");
                 }

                 backCategories[index] = backCategories[index].substring(0, backCategories[index].length() - 2);
            }
        }

		if (ActionContext.getContext().getSession().get("uploadedFiles") != null) {
			this.uploadedFiles = (List<UploadedFile>) ActionContext
					.getContext().getSession().get("uploadedFiles");
		}

		try {
			if (this.thumbnailId != null) {
				File file = new File(System.getProperty("java.io.tmpdir")
						+ "\\"
						+ this.uploadedFiles.get(this.thumbnailId).getName());
				this.items.setThumbImage(CommonUtil.getBytesFromFile(file));
			}

			if (this.gallaryId != null) {
				File file = new File(System.getProperty("java.io.tmpdir")
						+ "\\"
						+ this.uploadedFiles.get(this.thumbnailId).getName());
				this.items.setGalleryImage(CommonUtil.getBytesFromFile(file));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.items
				.setLocationCategoriesByRegion(LocationCategoriesServiceDelegator
						.getWOS(this.items.getLocationCategoriesByRegion()
								.getCategoryId()));
		this.items
				.setLocationCategoriesByCountry(LocationCategoriesServiceDelegator
						.getWOS(this.items.getLocationCategoriesByCountry()
								.getCategoryId()));
		this.items
				.setLocationCategoriesByState(LocationCategoriesServiceDelegator
						.getWOS(this.items.getLocationCategoriesByState()
								.getCategoryId()));
		this.items.setItemLanguage(TranslationSystemServiceDelegator.get(this.items.getItemLanguage().getLanguageCode()));

		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.setAttribute("id", 7);
		List<Itemtype> itemTypeList = (List<Itemtype>) CommonFunctionDelegator
				.getObjectBySearchCriteria(searchCriteria, Itemtype.class);
		if (itemTypeList != null)
			this.items.setItemtype((Itemtype) itemTypeList.get(0));

		ActionContext.getContext().getSession().put("items", this.items);
		ActionContext.getContext().getSession().put("categories", categories);
		ActionContext.getContext().getSession().put("backCategories", this.backCategories);
		
		this.items.setItemLanguage(TranslationSystemServiceDelegator.get(this.items.getItemLanguage().getLanguageCode()));

		feesCalculation(7);

		return SUCCESS;
	}

	public String previewAdvertisement() {
		this.backAction = "backToAdvertisement";

		if (request.getParameter("cid") != null
				&& request.getParameter("action").trim().equalsIgnoreCase(
						"submitsimilar")) {
			String itemId = request.getParameter("cid");
			this.items = ItemsServiceDelegator.getItemsById(Integer
					.parseInt(itemId));
		}

		if (request.getParameter("action") != null
				&& request.getParameter("action").equalsIgnoreCase(
						"copyAdvertisement")) {
			this.action = "copyAdvertisement";
		} else if (request.getParameter("items.itemId") != null) {
			this.action = "updateAdvertisement";
		} else {
			this.action = "submitAdvertisement";
		}

		categories = request.getParameterValues("categoryId");
		this.backCategories = this.categories;

		if(categories == null){
			categories = new String[1];
			categories[0] = "";
		}

		for (int index = 0; index < categories.length; index++) {
			DynamicTreeSelectAction dynamicTreeSelectAction = new DynamicTreeSelectAction();
			String[] categoryList = dynamicTreeSelectAction
					.getGeneralCategoriesFromCategoryId(Integer
							.parseInt(categories[index]));
			categories[index] = "";
			for (String str : categoryList) {
				categories[index] += (str + "->");
			}
			categories[index] = categories[index].substring(0,
					categories[index].length() - 2);
		}

		if (ActionContext.getContext().getSession().get("uploadedFiles") != null) {
			this.uploadedFiles = (List<UploadedFile>) ActionContext
					.getContext().getSession().get("uploadedFiles");
		}

		try {
			if (this.thumbnailId != null) {
				File file = new File(System.getProperty("java.io.tmpdir")
						+ "\\"
						+ this.uploadedFiles.get(this.thumbnailId).getName());
				this.items.setThumbImage(CommonUtil.getBytesFromFile(file));
			}

			if (this.gallaryId != null) {
				File file = new File(System.getProperty("java.io.tmpdir")
						+ "\\"
						+ this.uploadedFiles.get(this.thumbnailId).getName());
				this.items.setGalleryImage(CommonUtil.getBytesFromFile(file));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.items
				.setLocationCategoriesByRegion(LocationCategoriesServiceDelegator
						.getWOS(this.items.getLocationCategoriesByRegion()
								.getCategoryId()));
		this.items
				.setLocationCategoriesByCountry(LocationCategoriesServiceDelegator
						.getWOS(this.items.getLocationCategoriesByCountry()
								.getCategoryId()));
		this.items
				.setLocationCategoriesByState(LocationCategoriesServiceDelegator
						.getWOS(this.items.getLocationCategoriesByState()
								.getCategoryId()));
		this.items.setItemLanguage(TranslationSystemServiceDelegator.get(this.items.getItemLanguage().getLanguageCode()));

		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.setAttribute("id", 5);
		List<Itemtype> itemTypeList = (List<Itemtype>) CommonFunctionDelegator
				.getObjectBySearchCriteria(searchCriteria, Itemtype.class);
		if (itemTypeList != null)
			this.items.setItemtype((Itemtype) itemTypeList.get(0));

		ActionContext.getContext().getSession().put("items", this.items);
		ActionContext.getContext().getSession().put("categories", request.getParameterValues("categoryId"));
		ActionContext.getContext().getSession().put("backCategories", this.backCategories);
		this.items.setItemLanguage(TranslationSystemServiceDelegator.get(this.items.getItemLanguage().getLanguageCode()));

		feesCalculation(5);

		return SUCCESS;
	}

	public String submitCreateWanted() {
		GeneralOptions generalOptions = (GeneralOptions) ActionContext.getContext().getApplication().get(AdminConstants.GENERAL_OPTIONS);
		User tmpUser = SessionUtil.getUser(this.request);
		fees = Double.valueOf(request.getParameter("feesCharge"));

		items = (Items) ActionContext.getContext().getSession().get("items");
		if(items == null){
			addActionMessage("You have alreday submitted this item once");
			return INPUT;
		}

		if(generalOptions.getMaximumAllowedNegativeBalance() > (tmpUser.getCurrentBalance() - fees)) {
			addActionMessage("You do not have enough balance.");
		} else {
			this.items = (Items) ActionContext.getContext().getSession().get("items");
			this.items.setUserId(tmpUser);
			this.items.setStartTime(DateUtil.getTodaysDate());
			this.items.setEndTime(DateUtil.addDate(this.items.getStartTime(), Calendar.DATE, Integer.parseInt(this.items.getDuration())));

			this.items.setStatus(new BidStatus(4));
			
			AuctionServiceDelegator.saveAuctionSell(items);

			Transactions transactions = new Transactions();
			transactions.setAmount(fees);
			transactions.setBalance(tmpUser.getCurrentBalance() - fees);
			transactions.setDate(DateUtil.getTodaysDate());
			transactions.setTitle(this.items.getTitle());
			transactions.setUser(tmpUser);
			transactions.setAmount(fees);
			CommonFunctionDelegator.generalInsertbyobj(transactions);

			if(ActionContext.getContext().getSession().get("uploadedFiles") != null) {
				uploadedFiles = (List<UploadedFile>) ActionContext.getContext().getSession().get("uploadedFiles");
				String path = servletContext.getRealPath("/upload");
				for (UploadedFile uploadedFile : uploadedFiles) {
					File srcFile = new File(System.getProperty("java.io.tmpdir") + "\\" + uploadedFile.getName());
					File destFile = new File(path + "\\" + uploadedFile.getName());

					try {
						FileUtils.copyFile(srcFile, destFile);
					} catch (IOException e) {
						e.printStackTrace();
					}

					ItemAuctionImageId itemAuctionImage = new ItemAuctionImageId();
					itemAuctionImage.setItems(this.items);
					itemAuctionImage.setUser(tmpUser);
					itemAuctionImage.setImagePath("upload/" + uploadedFile.getName());
					itemAuctionImage.setContentType(uploadedFile.getContentType());

					ItemAuctionImage itemAuctionImage2 = new ItemAuctionImage();
					itemAuctionImage2.setId(itemAuctionImage);
					CommonFunctionDelegator.generalInsertbyobj(itemAuctionImage2);
				}
			}

			String[] categories = (String[]) ActionContext.getContext().getSession().get("categories");
			for (String categoryId : categories) {
				ItemCategories itemCategories = new ItemCategories();
				itemCategories.setCategoryId(CategoryManagerServiceDelegator.getWOS(Integer.parseInt(categoryId)));
				itemCategories.setItemId(this.items);
				CommonFunctionDelegator.generalInsertbyobj(itemCategories);
			}
			SessionUtil.getUser(request).setCurrentBalance(SessionUtil.getUser(request).getCurrentBalance() - fees);
			CommonFunctionDelegator.generalUpdatebyobj(SessionUtil.getUser(request));
			request.getSession().setAttribute("itemId", this.items.getItemId());
			ActionContext.getContext().getSession().put("items", null);
			ActionContext.getContext().getSession().put("categories", null);
		}
		return SUCCESS;
	}

	public String submitTender() {
		GeneralOptions generalOptions = (GeneralOptions) ActionContext.getContext().getApplication().get(AdminConstants.GENERAL_OPTIONS);
		User tmpUser = SessionUtil.getUser(this.request);
		fees = Double.valueOf(request.getParameter("feesCharge"));

		items = (Items) ActionContext.getContext().getSession().get("items");
		if(items == null){
			addActionMessage("You have alreday submitted this item once");
			return INPUT;
		}

		if(generalOptions.getMaximumAllowedNegativeBalance() > (tmpUser.getCurrentBalance() - fees)) {
			addActionMessage("You do not have enough balance.");
		} else {
			this.items = (Items) ActionContext.getContext().getSession().get("items");
			this.items.setUserId(tmpUser);
			this.items.setStartTime(DateUtil.getTodaysDate());
			this.items.setEndTime(DateUtil.addDate(this.items.getStartTime(), Calendar.MONTH, Integer.parseInt(this.items.getDuration())));

			this.items.setStatus(new BidStatus(4));
			
			AuctionServiceDelegator.saveAuctionSell(items);

			Transactions transactions = new Transactions();
			transactions.setAmount(fees);
			transactions.setBalance(tmpUser.getCurrentBalance() - fees);
			transactions.setDate(DateUtil.getTodaysDate());
			transactions.setTitle(this.items.getTitle());
			transactions.setUser(tmpUser);
			transactions.setAmount(fees);
			CommonFunctionDelegator.generalInsertbyobj(transactions);

			if(ActionContext.getContext().getSession().get("uploadedFiles") != null) {
				uploadedFiles = (List<UploadedFile>) ActionContext.getContext().getSession().get("uploadedFiles");
				String path = servletContext.getRealPath("/upload");
				for (UploadedFile uploadedFile : uploadedFiles) {
					File srcFile = new File(System.getProperty("java.io.tmpdir") + "\\" + uploadedFile.getName());
					File destFile = new File(path + "\\" + uploadedFile.getName());

					try {
						FileUtils.copyFile(srcFile, destFile);
					} catch (IOException e) {
						e.printStackTrace();
					}

					ItemAuctionImageId itemAuctionImage = new ItemAuctionImageId();
					itemAuctionImage.setItems(this.items);
					itemAuctionImage.setUser(tmpUser);
					itemAuctionImage.setImagePath("upload/" + uploadedFile.getName());
					itemAuctionImage.setContentType(uploadedFile.getContentType());

					ItemAuctionImage itemAuctionImage2 = new ItemAuctionImage();
					itemAuctionImage2.setId(itemAuctionImage);
					CommonFunctionDelegator.generalInsertbyobj(itemAuctionImage2);
				}
			}

			String[] categories = (String[]) ActionContext.getContext().getSession().get("categories");
			for (String categoryId : categories) {
				ItemCategories itemCategories = new ItemCategories();
				itemCategories.setCategoryId(CategoryManagerServiceDelegator.getWOS(Integer.parseInt(categoryId)));
				itemCategories.setItemId(this.items);
				CommonFunctionDelegator.generalInsertbyobj(itemCategories);
			}
			SessionUtil.getUser(request).setCurrentBalance(SessionUtil.getUser(request).getCurrentBalance() - fees);
			CommonFunctionDelegator.generalUpdatebyobj(SessionUtil.getUser(request));
			request.getSession().setAttribute("itemId", this.items.getItemId());
			ActionContext.getContext().getSession().put("items", null);
			ActionContext.getContext().getSession().put("categories", null);
		}
		return SUCCESS;
	}

	public String submitAdvertisement() {
		GeneralOptions generalOptions = (GeneralOptions) ActionContext.getContext().getApplication().get(AdminConstants.GENERAL_OPTIONS);
		User tmpUser = SessionUtil.getUser(this.request);
		fees = Double.valueOf(request.getParameter("feesCharge"));

		items = (Items) ActionContext.getContext().getSession().get("items");
		if(items == null){
			addActionMessage("You have alreday submitted this item once");
			return INPUT;
		}

		if(generalOptions.getMaximumAllowedNegativeBalance() > (tmpUser.getCurrentBalance() - fees)) {
			addActionMessage("You do not have enough balance.");
		} else {
			this.items = (Items) ActionContext.getContext().getSession().get("items");
			this.items.setUserId(tmpUser);
			this.items.setStartTime(DateUtil.getTodaysDate());
			this.items.setEndTime(DateUtil.addDate(this.items.getStartTime(), Calendar.DATE, Integer.parseInt(this.items.getDuration())));

			AuctionServiceDelegator.saveAuctionSell(items);

			Transactions transactions = new Transactions();
			transactions.setAmount(fees);
			transactions.setBalance(tmpUser.getCurrentBalance() - fees);
			transactions.setDate(DateUtil.getTodaysDate());
			transactions.setTitle(this.items.getTitle());
			transactions.setUser(tmpUser);
			transactions.setAmount(fees);
			CommonFunctionDelegator.generalInsertbyobj(transactions);

			if(ActionContext.getContext().getSession().get("uploadedFiles") != null) {
				uploadedFiles = (List<UploadedFile>) ActionContext.getContext().getSession().get("uploadedFiles");
				String path = servletContext.getRealPath("/upload");
				for (UploadedFile uploadedFile : uploadedFiles) {
					File srcFile = new File(System.getProperty("java.io.tmpdir") + "\\" + uploadedFile.getName());
					File destFile = new File(path + "\\" + uploadedFile.getName());

					try {
						FileUtils.copyFile(srcFile, destFile);
					} catch (IOException e) {
						e.printStackTrace();
					}

					ItemAuctionImageId itemAuctionImage = new ItemAuctionImageId();
					itemAuctionImage.setItems(this.items);
					itemAuctionImage.setUser(tmpUser);
					itemAuctionImage.setImagePath("upload/" + uploadedFile.getName());
					itemAuctionImage.setContentType(uploadedFile.getContentType());

					ItemAuctionImage itemAuctionImage2 = new ItemAuctionImage();
					itemAuctionImage2.setId(itemAuctionImage);
					CommonFunctionDelegator.generalInsertbyobj(itemAuctionImage2);
				}
			}

			String[] categories = (String[]) ActionContext.getContext().getSession().get("categories");
			for (String categoryId : categories) {
				ItemCategories itemCategories = new ItemCategories();
				itemCategories.setCategoryId(CategoryManagerServiceDelegator.getWOS(Integer.parseInt(categoryId)));
				itemCategories.setItemId(this.items);
				CommonFunctionDelegator.generalInsertbyobj(itemCategories);
			}
			SessionUtil.getUser(request).setCurrentBalance(SessionUtil.getUser(request).getCurrentBalance() - fees);
			CommonFunctionDelegator.generalUpdatebyobj(SessionUtil.getUser(request));
			request.getSession().setAttribute("itemId", this.items.getItemId());
			ActionContext.getContext().getSession().put("items", null);
		}
		return SUCCESS;
	}

	public String updateCreateWanted() throws Exception {
		Items item = ItemsServiceDelegator.getItemsById(this.items.getItemId());
		this.items = (Items) ActionContext.getContext().getSession().get("items");
		this.items.setUserId(item.getUserId());
		this.items.setItemtype(item.getItemtype());
		this.items.setStatus(new BidStatus(4));
		CommonFunctionDelegator.generalUpdatebyobj(this.items);
		return SUCCESS;
	}
	
	public String updateCreateTender() throws Exception {
		Items item = ItemsServiceDelegator.getItemsById(this.items.getItemId());
		this.items = (Items) ActionContext.getContext().getSession().get(
				"items");
		this.items.setUserId(item.getUserId());
		//this.items.setGeneralCategories(item.getGeneralCategories());
		this.items.setItemtype(item.getItemtype());
		CommonFunctionDelegator.generalUpdatebyobj(this.items);
		return SUCCESS;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.opensymphony.xwork2.ActionSupport#validate()
	 */
	@Override
	public void validate() {
		setDefaultValues();
		ListingOptions listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);
		languageList  = (List<Languages>) ActionContext.getContext().getApplication().get(AdminConstants.LANGUAGES);
		String[] words = listingOptions.getBannedWords().split(",");

		List<AdditionalCategories> additionalCategoriesList = (List<AdditionalCategories>) ActionContext.getContext().getApplication().get(AdminConstants.CATEGORY_FEES);

		if(this.items.getTitle().length() > listingOptions.getMaximumTitleSizeInBytes().intValue()) {
			addFieldError("items.title", "Title length can't be more than " + listingOptions.getMaximumTitleSizeInBytes().intValue());
		} else if (CommonUtil.isContainsBannedWords(words, this.items.getTitle())) {
			addFieldError("items.title", "Title contains banned word(s).");
		}

		if(this.items.getSubTitle().length() > listingOptions.getShortTitleSize().intValue()) {
			addFieldError("items.subTitle", "Sub-Title length can't be more than " + listingOptions.getShortTitleSize().intValue());
		} else if (CommonUtil.isContainsBannedWords(words, this.items.getSubTitle())) {
			addFieldError("items.subTitle", "Sub-Title contains banned word(s).");
		}

		if(this.items.getDescription().length() > listingOptions.getMaximumDescriptionSizeInBytes().intValue()) {
			addFieldError("items.description", "Description length can't be more than " + listingOptions.getMaximumDescriptionSizeInBytes().intValue());
		} else if (CommonUtil.isContainsBannedWords(words, this.items.getDescription())) {
			addFieldError("items.description", "Description contains banned word(s).");
		}

		if (request.getParameter("type") != null
				&& request.getParameter("categoryId") != null) {
			int type = Integer.parseInt(request.getParameter("type"));

			if (type == 4) {
				if (request.getParameterValues("categoryId").length > (int) additionalCategoriesList
						.get(0).getWanted().doubleValue()) {
					addActionError("You can not select more than "
							+ (int) additionalCategoriesList.get(0).getWanted()
									.doubleValue() + " Categories.");
				}
			} else if (type == 7) {
				if (request.getParameterValues("categoryId").length > (int) additionalCategoriesList
						.get(0).getWanted().doubleValue()) {
					addActionError("You can not select more than "
							+ (int) additionalCategoriesList.get(0).getWanted()
									.doubleValue() + " Categories.");
				}
			}
		} else {
			addActionError("You must have to select a single category...");
		}
	}

	private void feesCalculation(int type) {
		this.fees = 0;
		int totalFiles = this.uploadedFiles.size();
		String method = "getWanted";
		if (type == 7)
			method = "getTender";
		else if (type == 5)
			method = "getAdvertisement";

		List<AdditionalOptionsFees> additionalOptionsFeesList = (List<AdditionalOptionsFees>) ActionContext
				.getContext().getApplication().get(AdminConstants.OPTIONS_FEES);
		List<AdditionalCategories> additionalCategoriesFeesList = (List<AdditionalCategories>) ActionContext
				.getContext().getApplication()
				.get(AdminConstants.CATEGORY_FEES);
		List<AuctionListingFees> auctionListingFeesList = (List<AuctionListingFees>) ActionContext
				.getContext().getApplication().get(
						AdminConstants.REGULAR_AUCTION_OPTIONS);
		MiscellaneousFees miscellaneousFees = (MiscellaneousFees) ActionContext
				.getContext().getApplication().get(
						AdminConstants.MISCELLANEOUS_FEES);

		for (AuctionListingFees auctionListingFees : auctionListingFeesList) {
			if (this.items.getStartBid() >= auctionListingFees
					.getStartingPriceFrom()
					&& this.items.getStartBid() <= auctionListingFees
							.getStartingPriceTo()) {
				this.regularInsertionFees = auctionListingFees.getAuction();
			}
		}

		if (this.items.getQuantity() != null && this.items.getQuantity() > 1) {
			this.regularInsertionFees += miscellaneousFees
					.getDutchAuctionsMaximumFees();
		}

		if (this.items.getHomePageFeatured() != null
				&& this.items.getHomePageFeatured()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(0), method).toString());
		}
		if (this.items.getCategoryFeatured() != null
				&& this.items.getCategoryFeatured()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(1), method).toString());
		}
		if (this.items.getGallery() != null && this.items.getGallery()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(2), method).toString());
		}
		if (this.items.getGalleryFeatured() != null
				&& this.items.getGalleryFeatured()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(3), method).toString());
		}
		if (this.items.getGalleryPlus() != null && this.items.getGalleryPlus()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(4), method).toString());
		}
		if (this.items.getSubTitle() != null
				&& this.items.getSubTitle().length() > 0) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(5), method).toString());
		}
		if (this.items.getBold() != null && this.items.getBold()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(6), method).toString());
		}
		if (this.items.getColored() != null
				&& this.items.getColored().length() > 0) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(7), method).toString());
		}
		if (this.items.getBgColor() != null
				&& this.items.getBgColor().length() > 0) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(8), method).toString());
		}
		if (this.items.getGiftIcon() != null
				&& this.items.getGiftIcon().split(";")[0].length() > 0) {
			if ((additionalOptionsFeesList.get(9).getAdvertisement() != null)
					|| (additionalOptionsFeesList.get(9).getCompanyProfile() != null)
					|| (additionalOptionsFeesList.get(9).getWanted() != null)) {
				this.fees += Double.valueOf(CommonUtil.callMethod(
						AdditionalOptionsFees.class,
						additionalOptionsFeesList.get(9), method).toString());
			}
		}
		if (totalFiles == 1) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(10), method).toString());
		} else if (totalFiles == 2) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(11), method).toString());
		} else if (totalFiles == 3) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(12), method).toString());
		} else if (totalFiles == 4) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(13), method).toString());
		} else if (totalFiles >= 5) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(14), method).toString());
		}
		if (this.items.getMediaShow() != null && this.items.getMediaShow() == 2) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(15), method).toString());
		}
		if (this.items.getHomePage() != null
				&& this.items.getHomePage().length() > 0) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(16), method).toString());
		}
		if (this.items.getCrossPromotion() == null) {
		} else {
			if ((!this.items.getCrossPromotion().equals(""))
					&& (!this.items.getCrossPromotion().equals(null))) {
				if (Integer.parseInt(this.items.getCrossPromotion()) == 3) {
					this.fees += Double.valueOf(CommonUtil.callMethod(
							AdditionalOptionsFees.class,
							additionalOptionsFeesList.get(17), method)
							.toString());
				} else if (Integer.parseInt(this.items.getCrossPromotion()) == 6) {
					this.fees += Double.valueOf(CommonUtil.callMethod(
							AdditionalOptionsFees.class,
							additionalOptionsFeesList.get(18), method)
							.toString());
				} else if (Integer.parseInt(this.items.getCrossPromotion()) == 9) {
					this.fees += Double.valueOf(CommonUtil.callMethod(
							AdditionalOptionsFees.class,
							additionalOptionsFeesList.get(19), method)
							.toString());
				}
			}
		}
		if (this.items.getPrivateAuction() != null
				&& this.items.getPrivateAuction()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(20), method).toString());
		}
		if (this.items.getFramed() != null && this.items.getFramed()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(21), method).toString());
		}
		if (this.items.getCompanyProfile() != null
				&& this.items.getCompanyProfile()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(
					AdditionalOptionsFees.class,
					additionalOptionsFeesList.get(22), method).toString());
		}

		double categoryFee = 0;
		if (request.getParameterValues("categoryId") == null) {

		} else {
			for (String category : request.getParameterValues("categoryId")) {
				int level = CategoryManagerServiceDelegator.get(
						Integer.parseInt(category)).getLevel();
				if (level == 2)
					categoryFee += (((this.regularInsertionFees + this.fees) * Double
							.valueOf(CommonUtil
									.callMethod(
											AdditionalCategories.class,
											additionalCategoriesFeesList.get(1),
											method).toString())) / 100);
				else if (level == 3)
					categoryFee += (((this.regularInsertionFees + this.fees) * Double
							.valueOf(CommonUtil
									.callMethod(
											AdditionalCategories.class,
											additionalCategoriesFeesList.get(2),
											method).toString())) / 100);
				if (level > 3)
					categoryFee += (((this.regularInsertionFees + this.fees) * Double
							.valueOf(CommonUtil
									.callMethod(
											AdditionalCategories.class,
											additionalCategoriesFeesList.get(3),
											method).toString())) / 100);
			}
		}

		this.fees += categoryFee;
	}

	/**
	 * Gets the country list by level.
	 *
	 * @return the country list by level
	 */
	public List<LocationCategoriesWOS> getRegionListByLevel() {
		SearchCriteria searchListCriteria = new SearchCriteria();

		searchListCriteria.setOrderBy("categoryName");
		searchListCriteria.setAsc(true);
		searchListCriteria.setAttribute(AdminConstants.LEVEL, 1);
		return (List<LocationCategoriesWOS>) CommonFunctionDelegator.getObjectBySearchCriteria(searchListCriteria, LocationCategoriesWOS.class);
	}

	private boolean isCorrectFileExtention(String extentions,
			String fileExtention) {
		boolean flag = false;

		if(fileExtention == null)
			return false;

		String str = fileExtention.substring(
				fileExtention.lastIndexOf(".") + 1, fileExtention.length());
		String[] tmpExtentions = extentions.split(" ");
		for (String tmpExtention : tmpExtentions) {
			if (tmpExtention.equalsIgnoreCase(str)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	private void setDefaultValues() {
		ListingOptions listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);
		GeneralOptions generalOptions = (GeneralOptions) ActionContext.getContext().getApplication().get(AdminConstants.GENERAL_OPTIONS);

		if(this.items == null) {
			defaultLang = listingOptions.getDefaultItemLanguage();
			defaultRegion = listingOptions.getDefaultRegion();
			defaultCountry = listingOptions.getDefaultCountry();
			defaultCurrency = generalOptions.getCurrency().getCode();
		}
		else {
			defaultLang = this.items.getItemLanguage().getLanguageCode();
			defaultRegion = this.items.getLocationCategoriesByRegion().getCategoryId();
			defaultCountry = this.items.getLocationCategoriesByCountry().getCategoryId();
			defaultCurrency = this.items.getCurrency().getCode();
		}

		this.countryList = LocationCategoriesServiceDelegator.getCountryListByRegionIdWOS(defaultRegion);
		this.stateList = LocationCategoriesServiceDelegator.getStateListByCountryIdWOS(defaultCountry);
	}

	public List<LocationCategoriesWOS> getRegionList() {
		return regionList;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public List<Currency> getCurrency() {
		return currency;
	}

	public List<LocationCategoriesWOS> getCountryList() {
		return countryList;
	}

	public void setCountryList(List<LocationCategoriesWOS> countryList) {
		this.countryList = countryList;
	}

	public List<LocationCategoriesWOS> getStateList() {
		return stateList;
	}

	public void setStateList(List<LocationCategoriesWOS> stateList) {
		this.stateList = stateList;
	}

	public void setRegionList(List<LocationCategoriesWOS> regionList) {
		this.regionList = regionList;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public double getFees() {
		return fees;
	}

	public double getRegularInsertionFees() {
		return regularInsertionFees;
	}

	public List<UploadedFile> getUploadedFiles() {
		return uploadedFiles;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public double getFileFees() {
		return fileFees;
	}

	public void setFileFees(double fileFees) {
		this.fileFees = fileFees;
	}

	public List<Items> getFeaturedItems() {
		return featuredItems;
	}

	public String[] getBackCategories() {
		return backCategories;
	}

	public String getBackAction() {
		return backAction;
	}

	public String getDefaultLang() {
		return defaultLang;
	}

	public int getDefaultRegion() {
		return defaultRegion;
	}

	public int getDefaultCountry() {
		return defaultCountry;
	}

	public String getDefaultCurrency() {
		return defaultCurrency;
	}

	public List<Languages> getLanguageList() {
		return languageList;
	}

	public void setLanguageList(List<Languages> languageList) {
		this.languageList = languageList;
	}
}
